//
//  JKHomeCell.swift
//  JKWeibo
//
//  Created by albert on 16/5/16.
//  Copyright © 2016年 albert. All rights reserved.
//  微博主页cell

import UIKit
import SDWebImage
import KILabel

class JKHomeCell: UITableViewCell {
    
    /// 头像
    @IBOutlet weak var iconImageView: UIImageView!
    /// 认证图标
    @IBOutlet weak var verifiedImageView: UIImageView!
    /// 用户昵称
    @IBOutlet weak var screenNameLabel: UILabel!
    /// vip会员图标
    @IBOutlet weak var vipImageView: UIImageView!
    /// 微博发布时间
    @IBOutlet weak var createTimeLabel: UILabel!
    /// 微博来源
    @IBOutlet weak var sourceLabel: UILabel!
    /// 微博正文
    @IBOutlet weak var textContentLabel: KILabel!
    /// 底部工具条
    @IBOutlet weak var bottomToolBarView: UIView!
    /// 微博配图collectionView
    @IBOutlet weak var picCollectionView: JKPicCollectionView!

    /// 转发正文
    @IBOutlet weak var forwardLabel: KILabel!
    
    /** 模型数据 */
    var viewModel: JKStatusViewModel? {
        didSet{
            // 1.设置头像
            iconImageView.setCircleHeaderIcon(viewModel?.status.user?.avatar_large ?? "")
            
            // 2.设置认证图标
            verifiedImageView.image = viewModel?.verified_image
            
            // 3.设置用户昵称
            screenNameLabel.text = viewModel?.status.user?.screen_name
            
            // 4.设置会员图标
            vipImageView.image = nil
            screenNameLabel.textColor = UIColor.blackColor()
            if let image = viewModel?.mbrankImage {
                vipImageView.image = image
                screenNameLabel.textColor = UIColor.orangeColor()
            }
            
            // 5.设置微博发布时间
            createTimeLabel.text = viewModel?.created_timeStr
            
            // 6.设置微博来源
            sourceLabel.text = viewModel?.source_str
            
            // 7.设置微博正文
            //textContentLabel.text = viewModel?.status.text
            textContentLabel.attributedText = JKEmojiKeyboardPackage.createMutableAttrString(viewModel?.status.text ?? "", font: textContentLabel.font)
            
            // 8.设置配图
            picCollectionView.viewModel = viewModel
            
            // 9.转发微博正文label
            if let text = viewModel?.forwardText {
                //forwardLabel.text = text
                forwardLabel.attributedText = JKEmojiKeyboardPackage.createMutableAttrString(text, font: forwardLabel.font)
            }
        }
    }
    
    //MARK: - 外部控制方法
    /** 计算行高 */
    func calculateRowHeight(viewModel: JKStatusViewModel) -> CGFloat{
        // 1.设置数据
        self.viewModel = viewModel
        
        // 2.更新UI
        self.layoutIfNeeded()
        
        // 3.返回底部视图的最大值
        return CGRectGetMaxY(bottomToolBarView.frame)
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        // Attach a block to be called when the user taps a user handle
        textContentLabel.userHandleLinkTapHandler = { label, handle, range in
            JKLog("\(handle)--\(range)")
        }
        
        // Attach a block to be called when the user taps a hashtag
        textContentLabel.hashtagLinkTapHandler = { label, hashtag, range in
            JKLog("\(hashtag)--\(range)")
        }
        
        // Attach a block to be called when the user taps a URL
        textContentLabel.urlLinkTapHandler = { label, url, range in
            JKLog("\(url)--\(range)")
        }
    }
    
    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
    }
}

